package com.hoppinzq.service;

import com.hoppinzq.aop.TraceDuridSQL;
import com.hoppinzq.bean.TestBean;
import com.hoppinzq.dao.CodeDao;
import com.hoppinzq.service.annotation.ServiceRegister;
import com.hoppinzq.service.annotation.ServiceRegisterMethod;
import com.hoppinzq.service.auth.AuthenticationNotCheckAuthorizer;
import com.hoppinzq.service.modification.SetterModificationManager;
import com.hoppinzq.service.serializer.HessionSerializer;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * @author ZhangQi
 */
@ServiceRegister(title = "测试服务", description = "用来测试RPC的服务",serializer = HessionSerializer.class,modification = SetterModificationManager.class)
public class TestServiceImpl implements TestService{

    @Autowired
    private HttpServletRequest request;
    @Autowired
    private HttpServletResponse response;
    @Autowired
    private SqlSession sqlSession;
    @Autowired
    private CodeDao codeDao;
    AuthenticationNotCheckAuthorizer authenticationNotCheckAuthorizer;

    @Override
    @ServiceRegisterMethod(title = "测试方法1",description = "返回测试信息")
    public String sayTest(String msg) {
        return "这是一个测试，信息："+msg;
    }

    @Override
    @ServiceRegisterMethod(title = "测试方法2",description = "展示当远程服务抛出异常或者报错的时候，本地是什么行为")
    public int getDev(int i, int j) {
        if(j<0) {
            throw new RuntimeException("j不能是0");
        }
        return i/j;
    }

    @Override
    @ServiceRegisterMethod(title = "测试方法3",description = "展示远程服务是如何接收文件流并处理的")
    public String fileTest(InputStream inputStream) {
        StringBuilder sb=new StringBuilder();
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line).append("\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sb.toString();
    }

    @Override
    @ServiceRegisterMethod(title = "测试方法4",description = "展示远程服务是如何接收（可序列化）实体类")
    public String getBeanName(TestBean testBean) {
        if(testBean==null) {
            throw new RuntimeException("testBean 不能为null");
        }
        if(testBean.getName()==null||"".equals(testBean.getName())) {
            throw new RuntimeException("name 不能为空");
        }
        testBean.setAge(18);
        return "姓名："+testBean.getName()+",年龄："+testBean.getAge();
    }

    @Override
    @TraceDuridSQL
    @ServiceRegisterMethod(title = "测试方法5",description = "展示远程服务是如何处理traceId的")
    public String getTraceId() {
        codeDao.queryBlogCount();
        try{
            System.out.println(codeDao.getBlogCgCount(1L));
        }catch (Exception ex){

        }
        return "qwe";
    }
}

